package com.guo.hui.dao.redis;

import java.util.Collection;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

public class RedisUtil {

	@Autowired
	private RedisTemplate redisTemplate;

	public Object getValue(String key) {
		Object object = redisTemplate.keys(key);
		return object;
	}

	public void setValue(String key, Object obj) {
		redisTemplate.opsForValue().set(key, obj);
	}

	public void setValueTime(String key, Object obj, long time) {
		// TimeUnit 主要用于通知基于时间的方法如何解释给定的计时参数 =SECONDS(秒)
		redisTemplate.opsForValue().set(key, obj, time, TimeUnit.SECONDS);
	}

	public Collection<String> keys(String pattern) {
		return redisTemplate.keys(pattern);
	}

	public void delete(String key) {
		redisTemplate.delete(key);
	}

	public void delete(Collection<String> key) {
		redisTemplate.delete(key);
	}

	/* ----------- string --------- */

	public void set(String key, Object obj) {
		if (obj == null) {
			return;
		}
		redisTemplate.opsForValue().set(key, obj);

	}

	public void setTime(String key, Object obj, Long timeout, TimeUnit unit) {
		if (obj == null) {
			return;
		}

		if (timeout != null) {
			redisTemplate.opsForValue().set(key, obj, timeout, unit);
		} else {
			redisTemplate.opsForValue().set(key, obj);
		}
	}

}
